package org.zjx.action.game;


import org.zjx.action.basic.ScreenshotAction;
import org.zjx.analysis.tap.*;
import org.zjx.core.IWindow;

import java.awt.*;
import java.awt.event.KeyEvent;

public class TabAction extends AbstractWindowAction {
    private BaseTabAnalysis analysis;
    private final String cityName;
    private final Point targetLocation;

    public TabAction(IWindow targetWindow, String cityName, Point targetLocation) {
        super(targetWindow, false);
        this.cityName = cityName;
        this.targetLocation = targetLocation;
    }

    private void analysis() {
        switch (cityName) {
            case "长安城":
                analysis = new CACTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "建邺城":
                analysis = new JYCTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "地府":
                analysis = new DFTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "大唐国境":
                analysis = new DTGJTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "大唐境外":
                analysis = new DTJWTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "大唐官府":
                analysis = new DTGFTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "方寸山":
                analysis = new FCSTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "傲来国":
                analysis = new ALGTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "长寿村":
                analysis = new CSCTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "东海海底":
                analysis = new DHHDTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "东海湾":
                analysis = new DHWTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "花果山":
                analysis = new HGSTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "江南野外":
                analysis = new JNYWTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "普陀山":
                analysis = new PTSTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "凌霄宝殿":
                analysis = new LXBDTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "宝象国":
                analysis = new BXGTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "石头城":
                analysis = new STCTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "天宫":
                analysis = new TGTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "西梁女国":
                analysis = new XNNGTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "五庄观":
                analysis = new WZGTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "化生寺":
                analysis = new HSSTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "女儿村":
                analysis = new NECTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "朱紫国":
                analysis = new ZZGTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "龙宫":
                analysis = new LGTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            case "幻境花果山":
                analysis = new HJHGSTabAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
                break;
            default:
                throw new IllegalArgumentException("Tab地图未采集：" + cityName);
        }
    }

    @Override
    public void open() {
        analysis();
        mouseAloofWindow(analysis.windowArea == null ? analysis.getOriginBounds() : analysis.conversion(analysis.windowArea, analysis.getRefImageArea()));
        if (analysis.isOpened()) return;
        hotkey(KeyEvent.VK_TAB);
        delay(200);
        flush();
        open();
    }

    @Override
    public void close() {
        analysis();
        if (!analysis.isOpened()) return;
        hotkey(KeyEvent.VK_TAB);
        flush();
        close();
    }

    @Override
    public void reSetWindow() {
        if (analysis.isTransborder()) {
            drag(analysis.conversion(analysis.windowArea, analysis.getRefImageArea()), analysis.conversion(analysis.getOriginBounds(), analysis.getRefImageArea()));
            analysis();
            open();
        }
    }

    @Override
    public void mouseAloofWindow(Rectangle rect) {
        if (analysis.mouseInRect(rect)) {
            Point safePoint = analysis.findLargestRemainingAreaCenter(rect);
            move(safePoint);
            analysis();
        }
    }

    @Override
    protected void doExecute() throws Exception {
        open();
        reSetWindow();
        while (analysis.getLocation() == null || !analysis.getLocation().equals(targetLocation)) {
            Point gameMousePoint = stateManager.getGameState().getGameMousePoint();
            if (analysis.getLocation() == null || gameMousePoint == null) {
                move(analysis.conversion(analysis.windowArea, analysis.getContentArea()));
            } else {
                int offsetX = targetLocation.x - analysis.getLocation().x;
                int offsetY = analysis.getLocation().y - targetLocation.y;
                move(gameMousePoint.x + offsetX, gameMousePoint.y + offsetY);
            }
            delay(100);
            new ScreenshotAction(targetWindow).execute();
            analysis();
        }
        targetWindow.execute(op -> {
            op.click(1);
            return null;
        });
        delay(200);

        close();
    }
}
